// pages/wallpaper_page/index.js
import {
  requst_get_getPicInfo,
  requst_get_likePicRecord
} from '../../utils/api.js'
import {
  adList
} from "../../utils/adConfig.js";
var app= getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList:[{'title':'投稿壁纸','cid':0},{'title':'投稿头像','cid':1}],
    wallpaper: [], //壁纸
    bannerInfos: [], //轮播
    hiddenTop: true,
    isLastPage1: false,
    current: 1,
    pageSize: 20,
    bannerVideo: adList().bannerVideo,
    groupId:null

  },

  /**
   * 生命周期函数--监听页面加载
   * 0--我的点赞 1--抖音快手精选，2--用户上传精选。
   */
  onLoad(options) {
    this.setData({
      groupId:options.type
    })
    if(this.data.groupId==0){
      this.getLikePicList()
    }else{
      this.getPicList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  onPageScroll: function (t) {
    t.scrollTop > 400 ? this.setData({
      hiddenTop: false
    }) : this.setData({
      hiddenTop: true
    });
  },
  getLikePicList:function(){
    var that = this;
    const params = {
      current: that.data.current,
      size: that.data.pageSize
    }
    requst_get_likePicRecord(params).then(res=>{
      if (that.data.current == res.data.pages) {
        that.setData({
          isLastPage1: true,
        })
      }
      that.setData({
        wallpaper: that.data.wallpaper.concat(res.data.records)
      })
      wx.stopPullDownRefresh();
    })
  },
  getPicList: function () {
    var that = this;
    const params = {
      picCategory: 1,
      groupId:that.data.groupId,
      current: that.data.current,
      size: that.data.pageSize
    }
    requst_get_getPicInfo(params).then(res => {
      if (that.data.current == res.data.pages) {
        that.setData({
          isLastPage1: true,
        })
      }
      that.setData({
        wallpaper: that.data.wallpaper.concat(res.data.records)
      })
      wx.stopPullDownRefresh();
    }).catch(err => {
      wx.stopPullDownRefresh();
    })
  },

  onPullDownRefresh: function () {
    this.setData({
        current: 1,
        wallpaper: [],
        isLastPage1: false,

      });
      if(this.data.groupId==0){
        this.getLikePicList()
      }else{
        this.getPicList()
      }
  },

  onReachBottom: function () {
    if (!this.data.isLastPage1) {
      this.setData({
        current: this.data.current + 1,
      })
      if(this.data.groupId==0){
        this.getLikePicList()
      }else{
        this.getPicList()
      }
    }
  },
  
  onShareAppMessage: function () {
    return {
      title: "手机壁纸、渣男壁纸、高清壁纸、情侣头像、男生女生头像！",
      path: "/pages/wallpaper/index"
    };
  },
  onShareTimeline: function () {
    return {
      title: "好友推荐你一个优质头像壁纸库！",
      path: "/pages/wallpaper/index"
    };
  },
  
  bindDetail: function (t) {
    var src = t.currentTarget.dataset.url
    var id = t.currentTarget.dataset.id
    wx.navigateTo({
      url: "/pages/detail/detail?picId=" +id + "&picPath=" + src ,
    });
  },
  goTop: function (t) {
    wx.pageScrollTo ? wx.pageScrollTo({
      scrollTop: 0
    }) : wx.showModal({
      title: "提示",
      content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
    });
  },

})